<?
/**
  * Geocode PHP Class
  * v.000000001
  * By Harper Reed
  * Harper@nata2.org
  * I am pretty awesome
  * 
  * I hate documenting my code. I cannot lie. But its pretty easy. 
  * I will make more documentation later. Basically you just do this:
  * 
  *
  * -----------------------------------------------------------
  *
  * Copyright 2007 Harper Reed
  * 
  * -----------------------------------------------------------
  *
  * LICENSE
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License (GPL)
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * To read the license please visit http://www.gnu.org/copyleft/gpl.html
  *
  */

/**
 * TODO:
 * - add function to access google geocode functionality as well
 * - add logic to differentiate failover to yahoo or google depending on result
 */

class geocode{

	/**
	 * @var string $yahoo_appid your developer id
	 */
    var $yahoo_appid = "devid";

	/**
	 * Class instantiator
	 */
    function geocode()
    {
    }

	/**
	 * function getGeocode
	 * @param string location
	 * @param array options
	 * @return array location
	 */
    function getGeocode($location, $options = array())
    {
        $this->loc= $this->yahoo_geo($location);
        return $this->loc;
    }
    
    /**
     * function yahoo_geo
     * @param string location
     * @return array geocoded-location
     */
	function yahoo_geo($location) {
		//build the yahoo geocode access url
    	$q = 'http://api.local.yahoo.com/MapsService/V1/geocode';
      	$q .= '?appid='.$this->yahoo_appid.'&location='.rawurlencode($location).'&output=php';
      	//the file you want to store the data into
      	$tmp = '/tmp/yws_geo_'.md5($q);
      	//the grab the results
      	$result = $this->request_cache($q, $tmp, 43200);
      	return $result;
    }

	/**
	 * function request_cache
	 * @param string url url to cache
	 * @param string dest_file filname for cache
	 * @param int timeout time out of cache
	 * @return string cachecontent
	 */
    function request_cache($url, $dest_file, $timeout=43200) 
    {
    	//check if the cache file exists
    	//god i hate file_exists - it seems like it would be
    	//inefficient - but i do not know the truth
    	if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout))
      	{
      		//if the file doesnt exists then grab the url
        	$stream = @file_get_contents($url);
        	$tmpf = tempnam('/tmp','YWS');
        	//store it in the tmp file
        	file_put_contents($tmpf, $stream);
        	//grab the tmp file
        	$content = file_get_contents($tmpf);
        	//rename the temp file to the dest_file
        	rename($tmpf, $dest_file);
      	}else{
      		//if the cache is there and is newish grab the dest_file
        	$content = file_get_contents($dest_file);
      	}
      	//if content is empty then fail
      	//if the content is not empty - unserialize it. 
      	if ($content =="")$content = false;
      	else $content = unserialize($content);
      	//return the content
      	return $content;
    }
}
?>
